package client;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.LinkedBlockingQueue;

import model.Message;
import model.MessageType;
import client.view.ClientViewController;

public class ClientMain
{
	protected static ClientViewController clientViewController;
	
	private static String username;
	private static Socket socket;
	private static ObjectOutputStream outputStream;
	private static MessageReceiverThread messageReceiver;
	
	private static LinkedBlockingQueue<Message> messageQueue = new LinkedBlockingQueue<Message>();
	
	public static void main(String[] args)
	{
		clientViewController = new ClientViewController();
		clientViewController.openConnectWindow();

		while (true) {
			try
			{
				Message message = messageQueue.take();
				
				switch (message.getType())
				{
					case LOGIN_ACCEPTED:
						// caso o login seja aceito, inicia o envio de mensagens
						clientViewController.printLog("Login accepted.");
						clientViewController.closeLoginWindow();
						clientViewController.openChatWindow();
						break;
					case LOGIN_REFUSED:
						// caso o login seja recusado, é pedido um novo nome de usuário
						clientViewController.printLog("Login already in use.");
						clientViewController.printLoginError("Login already in use.");
						break;
					case LIST_ONLINE_CLIENTS:
						clientViewController.populateListOnlineClients(message.getText());
						break;
					case TEXT_MESSAGE_TO_CLIENT:
						clientViewController.showMessage(message.getFrom(), message.getText());
						break;
					case LOGOUT_ACK:
						try
						{
							closeSocket();
						}
						catch (IOException e)
						{
							e.printStackTrace();
						}
						System.exit(0);
						break;
					default:
						break;
				}			
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
	}

	public static void openSocket(final String host, final int port) throws UnknownHostException, IOException
	{
		socket = new Socket(host, port);
		outputStream = new ObjectOutputStream(socket.getOutputStream());
		
		messageReceiver = new MessageReceiverThread(socket);
		messageReceiver.start();
	}
	
	public static void closeSocket() throws IOException
	{
		messageReceiver.close();
		outputStream.close();
		socket.close();
	}

	public static void sendUsernameMessage(final String username)
	{
		// envia mensagem para o servidor com o
		// nome de usuário que se deseja logar
		setUsername(username);
		sendMessage(new Message(MessageType.VERIFY_LOGIN, username));
	}
	
	public static void sendTextMessage(final String text, final String to)
	{
		Message message = new Message(MessageType.TEXT_MESSAGE_TO_CLIENT, text, to);
		sendMessage(message);
	}
	
	public static void sendLogoutRequestMessage()
	{
		Message message = new Message(MessageType.LOGOUT_REQUEST);
		sendMessage(message);
	}
	
	private static void sendMessage(final Message message)
	{
		try
		{
			outputStream.writeObject(message);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	protected static void addMessage(final Message message)
	{
		try
		{
			messageQueue.put(message);
		}
		catch (InterruptedException e)
		{
			e.printStackTrace();
		}
	}

	public static String getUsername()
	{
		return username;
	}

	private static void setUsername(String username)
	{
		ClientMain.username = username;
	}
}
